/*******************************************************************************
* CHIPSEA F60 DELAY HAL
*******************************************************************************/

#include "hal_cs_f60_delay.h"

#ifdef CS_F60

#include <stdint.h>

/*******************************************************************************
Brief initialize the systick module to realize delay,1m slice is provided 
@param : none
********************************************************************************/
void Delay_Init(void)
{
	RCC_AHBPeriphClOCKCmd(RCC_AHBPeriph_SysTick, ENABLE);    //enable AHB 
	RCC_SysTickCLKSourceConfig(RCC_CLKSource_LIRC);     //Configure source clock

	SysTick_InitTypeDef Systick_InitStrucut;

	Systick_InitStrucut.SysTick_IEEnable = DELAY_SYSTICK_IEENABLE;	//DISABLE;  //disable systick interrupt  
	Systick_InitStrucut.SysTick_Reload   = DELAY_SYSTICK_RELOAD;	//10;       //1ms

	SysTick_Init(&Systick_InitStrucut);  

	SysTick_Disable();   
}

/********************************************************************************
Brief initialize the systick module to realize delay,1m slice is provided 
Example : Delay_ms(10) -- delay 10 ms
@param  : n if you want delay 10 ms , assigne n as 10  
*********************************************************************************/	
void hal_delay_ms(unsigned int n)
{
	uint32_t ReloadVal = (n * 10);
	//check the argument
	if(ReloadVal >> 24)
	{
		//LOG_INFO("Input argument error");
		return ;
	}

	SYST -> SYST_RVR = ReloadVal;   
	SYST -> SYST_CVR = 0;
	SysTick_Enable();

	while(!((SYST -> SYST_CSR >> 16) & 0x01)) { }

	SysTick_Disable();
}

void hal_delay_us(unsigned int n)
{
	uint32_t ReloadVal = 2; // Delay 200us
	//check the argument
	if(ReloadVal >> 24)
	{
		//LOG_INFO("Input argument error");
		return ;
	}

	SYST -> SYST_RVR = ReloadVal;   
	SYST -> SYST_CVR = 0;
	SysTick_Enable();

	while(!((SYST -> SYST_CSR >> 16) & 0x01)) {}

	SysTick_Disable();
}

#endif /* End of CS_F60 */
